import { _decorator, Component, tween, UIOpacity } from 'cc';
import { Ins } from '../Ins';
const { ccclass, property } = _decorator;

@ccclass('FadeMgr')
export class FadeMgr extends Component {
    opacityComp: UIOpacity = null

    onLoad() {
        Ins.initFadeMgr(this)
        this.node.active = false
        this.opacityComp = this.node.getComponent(UIOpacity)
    }

    isActive() {
        return this.node.active
    }

    fadeIn(seconds: number = 0.2) {
        return new Promise(resolve => {
            this.node.active = true
            this.opacityComp.opacity = 0
            tween(this.opacityComp).to(seconds, { opacity: 255 }).call(() => {
                resolve(null)
            }).start()
        })
    }

    fadeOut(seconds: number = 0.2) {
        return new Promise(resolve => {
            this.opacityComp.opacity = 255
            tween(this.opacityComp).to(seconds, { opacity: 0 }).call(() => {
                this.node.active = false
                resolve(null)
            }).start()
        })
    }
}


